{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Collecting python-moa\n",
      "  Downloading https://files.pythonhosted.org/packages/3b/cd/349176401c49ca749f3b2c6bc9523d4ce7486be5a6765979066bf1d582c4/python-moa-0.5.1.tar.gz\n",
      "Collecting astunparse (from python-moa)\n",
      "  Using cached https://files.pythonhosted.org/packages/2e/37/5dd0dd89b87bb5f0f32a7e775458412c52d78f230ab8d0c65df6aabc4479/astunparse-1.6.2-py2.py3-none-any.whl\n",
      "Requirement already satisfied: six<2.0,>=1.6.1 in /usr/local/miniconda3/envs/metadsl/lib/python3.7/site-packages (from astunparse->python-moa) (1.12.0)\n",
      "Requirement already satisfied: wheel<1.0,>=0.23.0 in /usr/local/miniconda3/envs/metadsl/lib/python3.7/site-packages (from astunparse->python-moa) (0.33.1)\n",
      "Building wheels for collected packages: python-moa\n",
      "  Building wheel for python-moa (setup.py) ... \u001b[?25lerror\n",
      "  Complete output from command /usr/local/miniconda3/envs/metadsl/bin/python -u -c \"import setuptools, tokenize;__file__='/private/var/folders/m7/t8dvwtnn32z84333p845tly40000gn/T/pip-install-i5nvugrj/python-moa/setup.py';f=getattr(tokenize, 'open', open)(__file__);code=f.read().replace('\\r\\n', '\\n');f.close();exec(compile(code, __file__, 'exec'))\" bdist_wheel -d /private/var/folders/m7/t8dvwtnn32z84333p845tly40000gn/T/pip-wheel-ti75xpel --python-tag cp37:\n",
      "  running bdist_wheel\n",
      "  running build\n",
      "  running build_py\n",
      "  creating build\n",
      "  creating build/lib\n",
      "  creating build/lib/moa\n",
      "  copying moa/exception.py -> build/lib/moa\n",
      "  copying moa/compiler.py -> build/lib/moa\n",
      "  copying moa/analysis.py -> build/lib/moa\n",
      "  copying moa/__init__.py -> build/lib/moa\n",
      "  copying moa/visualize.py -> build/lib/moa\n",
      "  copying moa/dnf.py -> build/lib/moa\n",
      "  copying moa/ast.py -> build/lib/moa\n",
      "  copying moa/shape.py -> build/lib/moa\n",
      "  copying moa/testing.py -> build/lib/moa\n",
      "  copying moa/array.py -> build/lib/moa\n",
      "  copying moa/onf.py -> build/lib/moa\n",
      "  creating build/lib/moa/frontend\n",
      "  copying moa/frontend/__init__.py -> build/lib/moa/frontend\n",
      "  copying moa/frontend/numpy.py -> build/lib/moa/frontend\n",
      "  copying moa/frontend/moa.py -> build/lib/moa/frontend\n",
      "  copying moa/frontend/array.py -> build/lib/moa/frontend\n",
      "  copying moa/frontend/__main__.py -> build/lib/moa/frontend\n",
      "  creating build/lib/moa/backend\n",
      "  copying moa/backend/__init__.py -> build/lib/moa/backend\n",
      "  copying moa/backend/python.py -> build/lib/moa/backend\n",
      "  installing to build/bdist.macosx-10.7-x86_64/wheel\n",
      "  running install\n",
      "  running install_lib\n",
      "  creating build/bdist.macosx-10.7-x86_64\n",
      "  creating build/bdist.macosx-10.7-x86_64/wheel\n",
      "  creating build/bdist.macosx-10.7-x86_64/wheel/moa\n",
      "  copying build/lib/moa/exception.py -> build/bdist.macosx-10.7-x86_64/wheel/moa\n",
      "  copying build/lib/moa/compiler.py -> build/bdist.macosx-10.7-x86_64/wheel/moa\n",
      "  copying build/lib/moa/analysis.py -> build/bdist.macosx-10.7-x86_64/wheel/moa\n",
      "  creating build/bdist.macosx-10.7-x86_64/wheel/moa/frontend\n",
      "  copying build/lib/moa/frontend/__init__.py -> build/bdist.macosx-10.7-x86_64/wheel/moa/frontend\n",
      "  copying build/lib/moa/frontend/numpy.py -> build/bdist.macosx-10.7-x86_64/wheel/moa/frontend\n",
      "  copying build/lib/moa/frontend/moa.py -> build/bdist.macosx-10.7-x86_64/wheel/moa/frontend\n",
      "  copying build/lib/moa/frontend/array.py -> build/bdist.macosx-10.7-x86_64/wheel/moa/frontend\n",
      "  copying build/lib/moa/frontend/__main__.py -> build/bdist.macosx-10.7-x86_64/wheel/moa/frontend\n",
      "  copying build/lib/moa/__init__.py -> build/bdist.macosx-10.7-x86_64/wheel/moa\n",
      "  creating build/bdist.macosx-10.7-x86_64/wheel/moa/backend\n",
      "  copying build/lib/moa/backend/__init__.py -> build/bdist.macosx-10.7-x86_64/wheel/moa/backend\n",
      "  copying build/lib/moa/backend/python.py -> build/bdist.macosx-10.7-x86_64/wheel/moa/backend\n",
      "  copying build/lib/moa/visualize.py -> build/bdist.macosx-10.7-x86_64/wheel/moa\n",
      "  copying build/lib/moa/dnf.py -> build/bdist.macosx-10.7-x86_64/wheel/moa\n",
      "  copying build/lib/moa/ast.py -> build/bdist.macosx-10.7-x86_64/wheel/moa\n",
      "  copying build/lib/moa/shape.py -> build/bdist.macosx-10.7-x86_64/wheel/moa\n",
      "  copying build/lib/moa/testing.py -> build/bdist.macosx-10.7-x86_64/wheel/moa\n",
      "  copying build/lib/moa/array.py -> build/bdist.macosx-10.7-x86_64/wheel/moa\n",
      "  copying build/lib/moa/onf.py -> build/bdist.macosx-10.7-x86_64/wheel/moa\n",
      "  running install_egg_info\n",
      "  running egg_info\n",
      "  writing python_moa.egg-info/PKG-INFO\n",
      "  writing dependency_links to python_moa.egg-info/dependency_links.txt\n",
      "  writing requirements to python_moa.egg-info/requires.txt\n",
      "  writing top-level names to python_moa.egg-info/top_level.txt\n",
      "  reading manifest file 'python_moa.egg-info/SOURCES.txt'\n",
      "  reading manifest template 'MANIFEST.in'\n",
      "  writing manifest file 'python_moa.egg-info/SOURCES.txt'\n",
      "  Copying python_moa.egg-info to build/bdist.macosx-10.7-x86_64/wheel/python_moa-0.5.1-py3.7.egg-info\n",
      "  running install_scripts\n",
      "  adding license file \"LICENSE\" (matched pattern \"LICEN[CS]E*\")\n",
      "  creating build/bdist.macosx-10.7-x86_64/wheel/python_moa-0.5.1.dist-info/WHEEL\n",
      "  creating '/private/var/folders/m7/t8dvwtnn32z84333p845tly40000gn/T/pip-wheel-ti75xpel/python_moa-0.5.1-cp37-none-any.whl' and adding 'build/bdist.macosx-10.7-x86_64/wheel' to it\n",
      "  Traceback (most recent call last):\n",
      "    File \"<string>\", line 1, in <module>\n",
      "    File \"/private/var/folders/m7/t8dvwtnn32z84333p845tly40000gn/T/pip-install-i5nvugrj/python-moa/setup.py\", line 42, in <module>\n",
      "      'Documentation': 'https://python-moa.readthedocs.io'\n",
      "    File \"/usr/local/miniconda3/envs/metadsl/lib/python3.7/site-packages/setuptools/__init__.py\", line 145, in setup\n",
      "      return distutils.core.setup(**attrs)\n",
      "    File \"/usr/local/miniconda3/envs/metadsl/lib/python3.7/distutils/core.py\", line 148, in setup\n",
      "      dist.run_commands()\n",
      "    File \"/usr/local/miniconda3/envs/metadsl/lib/python3.7/distutils/dist.py\", line 966, in run_commands\n",
      "      self.run_command(cmd)\n",
      "    File \"/usr/local/miniconda3/envs/metadsl/lib/python3.7/distutils/dist.py\", line 985, in run_command\n",
      "      cmd_obj.run()\n",
      "    File \"/usr/local/miniconda3/envs/metadsl/lib/python3.7/site-packages/wheel/bdist_wheel.py\", line 254, in run\n",
      "      wf.write_files(archive_root)\n",
      "    File \"/usr/local/miniconda3/envs/metadsl/lib/python3.7/site-packages/wheel/wheelfile.py\", line 123, in write_files\n",
      "      self.write(path, arcname)\n",
      "    File \"/usr/local/miniconda3/envs/metadsl/lib/python3.7/site-packages/wheel/wheelfile.py\", line 134, in write\n",
      "      zinfo = ZipInfo(arcname or filename, date_time=get_zipinfo_datetime(st.st_mtime))\n",
      "    File \"/usr/local/miniconda3/envs/metadsl/lib/python3.7/zipfile.py\", line 357, in __init__\n",
      "      raise ValueError('ZIP does not support timestamps before 1980')\n",
      "  ValueError: ZIP does not support timestamps before 1980\n",
      "  \n",
      "  ----------------------------------------\n",
      "\u001b[31m  Failed building wheel for python-moa\u001b[0m\n",
      "\u001b[?25h  Running setup.py clean for python-moa\n",
      "Failed to build python-moa\n",
      "Installing collected packages: astunparse, python-moa\n",
      "  Running setup.py install for python-moa ... \u001b[?25ldone\n",
      "\u001b[?25hSuccessfully installed astunparse-1.6.2 python-moa-0.5.1\n"
     ]
    }
   ],
   "source": [
    "!pip install python-moa"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "import dataclasses\n",
    "from typing import *\n",
    "import mypy_extensions"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "NodeSymbol = int # / enum"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "shape(OuterProduct(a, b)) == shape(a) ++ shape(b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Shape is a list of scalars\n",
    "# Every node has a shape, an operations, children ec."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "Node('VECTOR', child=(Node(1), Node(\"_i0\")))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "@dataclasses.dataclass\n",
    "class Node:\n",
    "    # Tuple is [\"REDUCE\", \"PLUS\"]\n",
    "    # Leaves have symbol \"ARRAY\"\n",
    "    symbol: Tuple[NodeSymbol, ...] \n",
    "    shape: # Node of type vector\n",
    "    # There are not nodes, all strings for now\n",
    "    attrib: Tuple[str, ...] #\n",
    "    child: \"Tuple[Node, ...]\"\n",
    "\n",
    "        \n",
    "\"INT\"\n",
    "\"FLOAT\" (1,) (123.0,)\n",
    "        \n",
    "Node(\"\")\n",
    "Shape = Optional[Tuple[typing.Union[int,  Node]]]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Nodes have two things, shape, and indexing.\n",
    "\n",
    "# First form is AST of operations.\n",
    "\n",
    "# You do work to\n",
    "# To compute the shapes\n",
    "# Collapse indexing"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# \"Tuple of indices\" is the Psi representation"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Array, shape, Tuple[striding per dimension],\n",
    "# and then the \"value\" either a reference or another array"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "Drop(2, (\"A\", (0, 10, 1)))\n",
    "==\n",
    "(\"A\", (2, 10, 1))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "ename": "TypeError",
     "evalue": "unsupported operand type(s) for |: 'str' and 'str'",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[0;32m<ipython-input-13-206d4ae23407>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0;34m@\u001b[0m\u001b[0mdataclasses\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mdataclass\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m      2\u001b[0m \u001b[0;32mclass\u001b[0m \u001b[0mSymbol\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m      3\u001b[0m     \u001b[0msymbol\u001b[0m\u001b[0;34m:\u001b[0m \u001b[0;34m\"ARRAY\"\u001b[0m \u001b[0;34m|\u001b[0m \u001b[0;34m\"\"\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;32m<ipython-input-13-206d4ae23407>\u001b[0m in \u001b[0;36mSymbol\u001b[0;34m()\u001b[0m\n\u001b[1;32m      1\u001b[0m \u001b[0;34m@\u001b[0m\u001b[0mdataclasses\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mdataclass\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m      2\u001b[0m \u001b[0;32mclass\u001b[0m \u001b[0mSymbol\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 3\u001b[0;31m     \u001b[0msymbol\u001b[0m\u001b[0;34m:\u001b[0m \u001b[0;34m\"ARRAY\"\u001b[0m \u001b[0;34m|\u001b[0m \u001b[0;34m\"\"\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[0;31mTypeError\u001b[0m: unsupported operand type(s) for |: 'str' and 'str'"
     ]
    }
   ],
   "source": [
    "# Symbol table is to \n",
    "\n",
    "@dataclasses.dataclass\n",
    "class Symbol:\n",
    "     # indices only show up referenced in symbol\n",
    "    symbol: \"ARRAY\"\n",
    "    shape: Shape\n",
    "    # some day might account for datatypes\n",
    "    type: None\n",
    "    # If value is None, then this has to be\n",
    "    # a function argument\n",
    "    # Then this is implicitly added as a function argument\n",
    "    value: Optional[Tuple[Union[int, float, Node]]]\n",
    "    # When are \n",
    "\n",
    "SymbolTable = Dict[str, Node]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def replace_shape_plus(a, b):\n",
    "    def result():\n",
    "        \n",
    "    return shape(a + b), lambda:  "
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
